package com.luo.chengrui.module.sa.controller.admin.auth;

import com.luo.chengrui.module.sa.controller.admin.auth.vo.*;
import com.luo.chengrui.module.sa.convert.auth.AuthorizeConvert;
import com.luo.chengrui.module.sa.dal.dataobject.auth.AuthorizeDO;
import com.luo.chengrui.module.sa.dal.dataobject.auth.AuthorizeViewDO;
import com.luo.chengrui.module.sa.service.auth.AuthorizeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 角色授权 ")
@RestController
@RequestMapping("/sa/authorize")
@Validated
public class AuthorizeController {

    @Resource
    private AuthorizeService authorizeService;

    @PostMapping("/create")
    @Operation(summary = "创建角色授权 ")
    @PreAuthorize("@ss.hasPermission('sa:authorize:create')")
    public CommonResult<String> createAuthorize(@Valid @RequestBody AuthorizeCreateReqVO createReqVO) {
        return success(authorizeService.createAuthorize(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色授权 ")
    @PreAuthorize("@ss.hasPermission('sa:authorize:update')")
    public CommonResult<Boolean> updateAuthorize(@Valid @RequestBody AuthorizeUpdateReqVO updateReqVO) {
        authorizeService.updateAuthorize(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色授权 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:authorize:delete')")
    public CommonResult<Boolean> deleteAuthorize(@RequestParam("id") String id) {
        authorizeService.deleteAuthorize(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色授权 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:authorize:query')")
    public CommonResult<AuthorizeRespVO> getAuthorize(@RequestParam("id") String id) {
        AuthorizeDO authorize = authorizeService.getAuthorize(id);
        return success(AuthorizeConvert.INSTANCE.convert(authorize));
    }

    @GetMapping("/list")
    @Operation(summary = "获得角色授权 列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:authorize:query')")
    public CommonResult<List<AuthorizeRespVO>> getAuthorizeList(@RequestParam("ids") Collection<String> ids) {
        List<AuthorizeDO> list = authorizeService.getAuthorizeList(ids);
        return success(AuthorizeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色授权 分页")
    @PreAuthorize("@ss.hasPermission('sa:authorize:query')")
    public CommonResult<PageResult<AuthorizeRespVO>> getAuthorizePage(@Valid AuthorizePageReqVO pageVO) {
        PageResult<AuthorizeViewDO> pageResult = authorizeService.selectPageCustomize(pageVO);
        return success(AuthorizeConvert.INSTANCE.convertPageCustomize(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出角色授权  Excel")
    @PreAuthorize("@ss.hasPermission('sa:authorize:export')")
    @OperateLog(type = EXPORT)
    public void exportAuthorizeExcel(@Valid AuthorizeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AuthorizeDO> list = authorizeService.getAuthorizeList(exportReqVO);
        // 导出 Excel
        List<AuthorizeExcelVO> datas = AuthorizeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "角色授权 .xls", "数据", AuthorizeExcelVO.class, datas);
    }

}
